#include <iostream>

#include <workdir.filesystem.Directory.h>
#include <workdir.filesystem.File.h>

using namespace std;
using namespace nemesis;
using namespace workdir;

void filesystem::Directory::print (const int level) const
   throw ()
{
   filesystem::Directory::const_child_iterator ii;
   filesystem::Directory::const_child_iterator maxii = child_end ();
   const filesystem::Directory* auxd;
   const filesystem::File* auxf; 
   int row = 0;
   bool hasDirectories = false;

   for (int space = 0; space < level; space ++)
      cout << "   ";

   cout << getName () << "/ (" << child_size () << "): " << endl;
   
   /* 
    * Recorre todas las dependencias y visualiza primero todos los ficheros
    */
   for (ii = child_begin (); ii != maxii; ii ++) {
      auxf = filesystem::File::down_cast (filesystem::Directory::child (ii));
      
      if (auxf == NULL) {
         hasDirectories = true;
         continue;
      }
      
      if (row == 0) {
         for (int space = 0; space < level +  1; space ++)
            cout << "   ";
      }
      
      auxf->print (level + 1);
      
      if (++ row == 5) {
         cout << endl;
         row = 0;
      }
   }

   if (row > 0)
      cout << endl;
   
   /* 
    * Trata los directorios recursivamente
    */
   for (ii = child_begin (); hasDirectories == true && ii != maxii; ii ++) {
      auxd = filesystem::Directory::down_cast (filesystem::Directory::child (ii));
      
      if (auxd != NULL)
         auxd->print (level + 1);
   }      
}
